package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.ParkInfoBean;

import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Caleb.F
 * on 2019-11-18
 * at 23:19
 */
public class MyMapRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mcontext;
    private List<ParkInfoBean.ROWSDETAILBean> mlist;
    private RecyclerView.ViewHolder mHolder;
    private static final Integer RECYCLER_TEXTVIEW = 0;
    private static final Integer RECYCLER_IMAGEVIEW = 1;
    private OnIvClickListener mOnIvClickListener;

    private int mPosition;

    public MyMapRecyclerViewAdapter(Context context, List<ParkInfoBean.ROWSDETAILBean> list) {
        mcontext = context;
        mlist = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == RECYCLER_TEXTVIEW) {
            return new MyTextViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.maprecitem, parent, false));
        } else {
            return new MyImageViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.map_item_im, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        updateList();
        if (holder instanceof MyTextViewHolder) {
            if (mlist.get(position).getOpen() == 0) {
                ((MyTextViewHolder) holder).mTvDetail.setText("关闭");
            } else {
                ((MyTextViewHolder) holder).mTvDetail.setText("已满");
            }
            ((MyTextViewHolder) holder).mTvParkerDetail.setText(mlist.get(position).getAddress() + Math.round(mlist.get(position).getDistance()) + " m");
            ((MyTextViewHolder) holder).mTvParkerName.setText(mlist.get(position).getName());
            ((MyTextViewHolder) holder).mTvParkerPay.setText("空车位" + mlist.get(position).getEmptySpace()
                    + ", 停车费" + mlist.get(position).getAverageRate()+ "元/小时");
            ((MyTextViewHolder) holder).mTvRouteId.setText(mlist.get(position).getParkerId() + "");
        } else {
            ((MyImageViewHolder) holder).mTvParkerDetail.setText(mlist.get(position).getAddress() + Math.round(mlist.get(position).getDistance()) +" m");
            ((MyImageViewHolder) holder).mTvParkerName.setText(mlist.get(position).getName());
            ((MyImageViewHolder) holder).mTvParkerPay.setText("空车位" + mlist.get(position).getEmptySpace()
                    + ", 停车费" + mlist.get(position).getAverageRate() + "元/小时");
            ((MyImageViewHolder) holder).mTvRouteId.setText(mlist.get(position).getParkerId() + "");
            ((MyImageViewHolder) holder).mIvArrow.setOnClickListener(v->{
                mOnIvClickListener.onIvClickListener(position);
            });
        }
    }

    private void updateList() {
        for (ParkInfoBean.ROWSDETAILBean item:
             mlist) {
            item.setDistance(getDistance(item.getCoordinate()));
        }
        Collections.sort(mlist,((o1,o2)-> Math.round(o1.getDistance())-Math.round(o2.getDistance())));
    }

    private Float getDistance(String coordinate) {
       return  AMapUtils.calculateLineDistance(new LatLng(Double.parseDouble(coordinate.split(",")[1]),
                Double.parseDouble(coordinate.split(",")[0])), new LatLng(39.941853, 116.385307));

    }

    private Integer checkforNumber(String str) {
        Pattern compile = Pattern.compile("[^0-9]");
        return Integer.parseInt(compile.matcher(str).replaceAll("").trim());
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }


    @Override
    public int getItemViewType(int position) {
        if (mlist.get(position).getOpen() == 0 || mlist.get(position).getEmptySpace() == 0) {
            return RECYCLER_TEXTVIEW;
        } else {
            return RECYCLER_IMAGEVIEW;
        }
    }

    public interface OnIvClickListener{
        void onIvClickListener(int pos);
    }
    public void setOnIvClickListener(OnIvClickListener onIvClickListener){
        mOnIvClickListener=onIvClickListener;
    }

    class MyTextViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mWholeLayout;
        TextView mTvRouteId;
        TextView mTvParkerName;
        TextView mTvParkerPay;
        TextView mTvParkerDetail;
        TextView mTvDetail;

        public MyTextViewHolder(View convertView) {
            super(convertView);
            mWholeLayout = convertView.findViewById(R.id.whole_layout);
            mTvRouteId = convertView.findViewById(R.id.tv_route_id);
            mTvParkerName = convertView.findViewById(R.id.tv_parker_name);
            mTvParkerPay = convertView.findViewById(R.id.tv_parker_pay);
            mTvParkerDetail = convertView.findViewById(R.id.tv_parker_detail);
            mTvDetail = convertView.findViewById(R.id.map_text);
        }
    }

    class MyImageViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mWholeLayout;
        TextView mTvRouteId;
        TextView mTvParkerName;
        TextView mTvParkerPay;
        TextView mTvParkerDetail;
        ImageView mIvArrow;

        public MyImageViewHolder(View convertView) {
            super(convertView);
            mWholeLayout = convertView.findViewById(R.id.whole_layout);
            mTvRouteId = convertView.findViewById(R.id.tv_route_id);
            mTvParkerName = convertView.findViewById(R.id.tv_parker_name);
            mTvParkerPay = convertView.findViewById(R.id.tv_parker_pay);
            mTvParkerDetail = convertView.findViewById(R.id.tv_parker_detail);
            mIvArrow = convertView.findViewById(R.id.iv_arrow);
        }
    }
}
